;; Configuration of Python IDE
;; https://github.com/jorgenschaefer/elpy
; (require 'elpy nil t)
 ;(elpy-enable)
 
;;快速定位行
(global-set-key [(C-f12)] 'goto-line)
;; 关闭工具栏，tool-bar-mode 即为一个 Minor Mode
(tool-bar-mode 0)
;;在minibuffer上面可以显示列号     
(column-number-mode t)
;;关闭菜单栏
(menu-bar-mode 0)
;; 关闭文件滑动控件
(scroll-bar-mode -1)


;;自动补全右边的部分.包括 "",  (), [] , {}等等。
(electric-pair-mode 1)
;; 显示行号
(global-linum-mode 1)

;; 更改光标的样式（不能生效，解决方案见第二集）
(setq cursor-type 'bar)

;; 关闭启动帮助画面
(setq inhibit-splash-screen 1)

;;语法高亮
(require 'org)
(setq org-src-fontify-natively t)
;;主题
;(color-theme-sitaramv-nt)
;; 关闭出错时的蜂鸣提示声
(setq visible-bell nil)

;; 尽快显示按键序列
(setq echo-keystrokes 0.1)
;;在minibuffer里启用自动补全函数和变量
(icomplete-mode 1)
;; Linux 下复制内容到系统的剪切板
(setq x-select-enable-clipboard t)
;; 启用部分补全功能，如输入M-x q r r相当于M-x query-replace-regexp
;;(partial-completion-mode 1)

;; 在emacs读man文档时，使用当前buffer
(setq Man-notify-method 'pushy)
;;启用备份
(setq version-control t)
;;备份最原始的版本两次，记第一次编辑前的文档，和第二次编辑前的文档
(setq kept-old-versions 2)
;;备份最新的版本2次，理解同上
(setq kept-new-versions 2)
;;删掉不属于以上7版本的版本
(setq delete-old-versions t)
;;设置备份文件的路径
(setq backup-directory-alist '(("." . "~/.emacs.d/backup")))
;;备份设置方法，直接拷贝
;;(setq backup-by-copying(global-set-key (kbd "C-c k") 
;; 不生成名为#filename#的临时文件
(setq auto-save-default nil)
;; 自动保存
(setq auto-save-mode t)
;;自动重载更改的文件
(global-auto-revert-mode 1)


;;光标靠近鼠标指针时，让鼠标指针自动让开，别挡住视线
(mouse-avoidance-mode 'animate)

;;支持emacs和外部程序的粘贴
(setq x-select-enable-clipboard t)

;; Tell Emacs fill commands to insert two after a colon
(setq colon-double-space t)

;; 在文档最后自动插入空白行
(setq require-final-newline t)

;;括号匹配时显示另外一边的括号，而不是烦人的跳到另一个括号
(show-paren-mode t)
(setq show-paren-style 'parentheses)

;;在标题栏显示buffer的名字.区分不同的主机和用户名称
(setq frame-title-format '((:eval (getenv-internal "LOGNAME"))"@"(:eval (system-name))":"(:eval (or (buffer-file-name) (buffer-name)))))

;;设置字体颜色为黑色，默认为黑色
(set-foreground-color "grey")
;;设置背景为黑色，默认为白色，你也可以自由设置其它颜色
(set-background-color "black")
;;设置光标的颜色为金黄，默认为黑色
(set-cursor-color "gold1")
;;设置鼠标的颜色
(set-mouse-color "gold1")

;; 设置语法高亮显示的背景和主题，区域选择的背景和主题，二次选择的背景和选择
(set-face-foreground 'highlight "white")
(set-face-background 'highlight "blue")
(set-face-foreground 'region "cyan")
(set-face-background 'region "blue")
(set-face-foreground 'secondary-selection "skyblue")
(set-face-background 'secondary-selection "darkblue")

;;启用时间显示设置，在minibuffer上面的那个杠上
(display-time-mode 1)

;;时间使用24小时制
(setq display-time-24hr-format t)

;;时间显示包括日期和具体时间
(setq display-time-day-and-date t)
(setq-default auto-fill-function 'do-auto-fill)
(setq show-paren-mode t)


;;能把一个代码块缩起来，需要的时候再展开
;; M-x hs-minor-mode
;; C-c @ ESC C-s show all
;; C-c @ ESC C-h hide all
;; C-c @ C-s show block
;; C-c @ C-h hide block
;; C-c @ C-c toggle hide/show
(load-library "hideshow")
(add-hook 'c-mode-hook 'hs-minor-mode)
(add-hook 'c++-mode-hook 'hs-minor-mode)
(add-hook 'java-mode-hook 'hs-minor-mode)
(add-hook 'perl-mode-hook 'hs-minor-mode)
(add-hook 'php-mode-hook 'hs-minor-mode)
(add-hook 'emacs-lisp-mode-hook 'hs-minor-mode)
(add-hook 'html-mode-hook 'hs-minor-mode)
(add-hook 'js-mode-hook 'hs-minor-mode)

;;todo
 (autoload 'remember "remember" nil t)
(autoload 'remember-region "remember" nil t)
(setq org-reverse-note-order t)
(when (file-exists-p "~/gtd/")
  (define-key global-map [(f7)] 'remember)
  (setq remember-annotation-functions '(org-remember-annotation))
  (setq remember-handler-functions '(org-remember-handler))
  (add-hook 'remember-mode-hook 'org-remember-apply-template)

  (setq org-directory "~/gtd/")
  (setq org-remember-templates
        `((?t "* TODO %?\n  %i"
              ,(expand-file-name "todo.org" org-directory) "Tasks")
          (?m "* %U\n\n  %?%i\n  %a"
              ,(expand-file-name "notes.org" org-directory) "Notes")))

  (let ((todo (expand-file-name "todo.org" org-directory)))
    (when (file-exists-p todo)
      (add-to-list 'org-agenda-files todo))))

;; C语言模式
(add-hook 'c-mode-hook
'(lambda ()

;;把c语言风格设置为k&r风格
(c-set-style "k&r")

;; 此模式下，当按Backspace时会删除最多的空格
(c-toggle-hungry-state)

;; TAB键的宽度设置为4
(setq c-basic-offset 4)

;; 在菜单中加入当前Buffer的函数索引
(imenu-add-menubar-index)

;; 在状态条上显示当前光标在哪个函数体内部
(which-function-mode)))

;; C++语言模式
(add-hook 'c++-mode-hook
'(lambda()

;;把C++语言风格设置为stroustrup风格
(c-set-style "stroustrup")
(c-toggle-auto-state)
(c-toggle-hungry-state)
(setq c-basic-offset 4)
(imenu-add-menubar-index)
(which-function-mode)))

;;设置f9调用speedbar命令
;;使用 n 和 p 可以上下移动，
;; + 展开目录或文件进行浏览，- 收缩，RET 访问目录或文件，g 更新 speedbar。
(global-set-key [f9] 'speedbar)


;; JAVA语言模式
(add-hook 'java-mode-hook
'(lambda()

(c-set-style "java")

(c-toggle-auto-state)

(c-toggle-hungry-state)

(setq c-basic-offset 4)

(imenu-add-menubar-index)

(which-function-mode)))


;;设置M-/作为标志位，默认C-@来setmark,C-@不太好用
(global-set-key (kbd "M-/") 'set-mark-command)
;; 设置F12 快速察看日程安排
(global-set-key [f12] 'list-bookmarks)
;;设置缺省主模式是text，,并进入auto-fill次模式.
;;而不是基本模式fundamental-mode
(setq default-major-mode 'text-mode)
(add-hook 'text-mode-hook 'turn-on-auto-fill)
;;让 Emacs 可以直接打开和显示图片。
(setq auto-image-file-mode t)

;;补全功能
(global-set-key (kbd "<f8>") 'hippie-expand)

(setq hippie-expand-try-functions-list
      '(try-expand-all-abbrevs try-expand-dabbrev
        try-expand-dabbrev-all-buffers try-expand-dabbrev-from-kill
        try-complete-lisp-symbol-partially try-complete-lisp-symbol
        try-complete-file-name-partially try-complete-file-name))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 实现不设置标志就能复制
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;复制单词

(defun copy-word (&optional arg)
"Copy words at point into kill-ring"
(interactive "P")
(copy-thing 'backward-word 'forward-word arg)
(paste-to-mark arg))
;;绑定键
(global-set-key (kbd "C-c w")         (quote copy-word))

;;复制一行
(defun copy-line (&optional arg)
"Save current line into Kill-Ring without mark the line "
(interactive "P")
(copy-thing 'beginning-of-line 'end-of-line arg)
(paste-to-mark arg))
;;绑定键
(global-set-key (kbd "C-c l")         (quote copy-line))

;;复制一段
(defun copy-paragraph (&optional arg)
"Copy paragraphes at point"
(interactive "P")
(copy-thing 'backward-paragraph 'forward-paragraph arg)
(paste-to-mark arg))
;;绑定键
(global-set-key (kbd "C-c p")(quote copy-paragraph))


;;复制字符串
(defun beginning-of-string(&optional arg)
"  "
(re-search-backward "[ \t]" (line-beginning-position) 3 1)
(if (looking-at "[\t ]")  (goto-char (+ (point) 1)) )
)

(defun end-of-string(&optional arg)
" "
(re-search-forward "[ \t]" (line-end-position) 3 arg)
(if (looking-back "[\t ]") (goto-char (- (point) 1)) )
)

(defun thing-copy-string-to-mark(&optional arg)
" Try to copy a string and paste it to the mark When used in shell-mode, it will paste string on shell prompt by default "
(interactive "P")
(copy-thing 'beginning-of-string 'end-of-string arg)
(paste-to-mark arg))
;;绑定键
(global-set-key (kbd "C-c s")         (quote thing-copy-string-to-mark))


(defun beginning-of-parenthesis(&optional arg)
"  "
(re-search-backward "[[<(?\"]" (line-beginning-position) 3 1)
(if (looking-at "[[<(?\"]")  (goto-char (+ (point) 1)) )
)

(defun end-of-parenthesis(&optional arg)
" "
(re-search-forward "[]>)?\"]" (line-end-position) 3 arg)
(if (looking-back "[]>)?\"]") (goto-char (- (point) 1)) )
)

(defun thing-copy-parenthesis-to-mark(&optional arg)
" Try to copy a parenthesis and paste it to the mark When used in shell-mode, it will paste parenthesis on shell prompt by default "
(interactive "P")
(copy-thing 'beginning-of-parenthesis 'end-of-parenthesis arg)
(paste-to-mark arg))
;;绑定键
(global-set-key (kbd "C-c a")         (quote thing-copy-parenthesis-to-mark))


;;所有以上几个函数，都依赖于下面这三个基础函数，由他们三个负责完成实际的工作。

(defun get-point (symbol &optional arg)
"get the point"
(funcall symbol arg)
(point))

(defun copy-thing (begin-of-thing end-of-thing &optional arg)
"copy thing between beg & end into kill ring"
(let ((beg (get-point begin-of-thing 1))
(end (get-point end-of-thing arg)))
(copy-region-as-kill beg end)))

(defun paste-to-mark(&optional arg)
"Paste things to mark, or to the prompt in shell-mode"
(let ((pasteMe 
(lambda()
(if (string= "shell-mode" major-mode)
(progn (comint-next-prompt 25535) (yank))
(progn (goto-char (mark)) (yank) )))))
(if arg
(if (= arg 1)
nil
(funcall pasteMe))
(funcall pasteMe))
))

(provide 'lgspython)